// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <cstdint>
#include <memory>
#include <vector>

namespace kumo::pollux::dwio::common {
    class LoadUnit {
    public:
        virtual ~LoadUnit() = default;

        /// Perform the IO (read)
        virtual void load() = 0;

        /// Unload the unit to free memory
        virtual void unload() = 0;

        /// Number of rows in the unit
        virtual uint64_t getNumRows() = 0;

        /// Number of bytes that the IO will read
        virtual uint64_t getIoSize() = 0;
    };

    class UnitLoader {
    public:
        virtual ~UnitLoader() = default;

        /// Must block until the unit is loaded. This call could unload other units.
        /// So the returned LoadUnit& is only guaranteed to remain loaded until the
        /// next call.
        virtual LoadUnit &getLoadedUnit(uint32_t unit) = 0;

        /// Reader reports progress calling this method. The call must be done
        /// **after** getLoadedUnit for unit.
        virtual void
        onRead(uint32_t unit, uint64_t rowOffsetInUnit, uint64_t rowCount) = 0;

        /// Reader reports seek calling this method. The call must be done **before**
        /// getLoadedUnit for the new unit.
        virtual void onSeek(uint32_t unit, uint64_t rowOffsetInUnit) = 0;
    };

    class UnitLoaderFactory {
    public:
        virtual ~UnitLoaderFactory() = default;

        virtual std::unique_ptr<UnitLoader> create(
            std::vector<std::unique_ptr<LoadUnit> > loadUnits,
            uint64_t rowsToSkip) = 0;
    };
} // namespace kumo::pollux::dwio::common
